package com.hd.trans.common;

import android.content.Context;

import com.google.gson.Gson;
import com.hd.trans.utils.CommonUtil;
import com.hd.trans.utils.DeviceUtil;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

import androidx.annotation.IntDef;

public class Constants {
    /**
     * 分组级别
     */
    public static class LevelType {
        public static final int GROUP = 0; // 组
        public static final int CHILD = 1; // 子
        public static final int HEADER = 2; // 头部
        public static final int FOOT = 3; // 脚部
        public static final int CHILD_TOP = 4; //
        public static final int CHILD_BOTTOM = 5; //
    }

    /**
     * 闪光灯状态
     */
    public static final int LIGHT_AUTO = 1; // 自动
    public static final int LIGHT_ON = 2; // 开
    public static final int LIGHT_OFF = 3; // 关

    /**
     * 文本翻译页状态
     */
    public static final int TRANSLATE_NORMAL = 0; // 初始状态(未翻译)
    public static final int TRANSLATING = 1; // 翻译中
    public static final int TRANSLATED = 2; // 翻译后

    @IntDef({TRANSLATE_NORMAL, TRANSLATING, TRANSLATED})
    @Retention(RetentionPolicy.SOURCE)
    public @interface TranslatePageState {
    }

    /**
     * 语音翻译页状态
     */
    public static final int VOICE_NORMAL = 0; // 普通状态
    public static final int VOICE_RECOGNIZING = 1; // 识别中
    public static final int VOICE_RECORD_PAUSE = 2; // 识别暂停
    @IntDef({VOICE_NORMAL, VOICE_RECOGNIZING, VOICE_RECORD_PAUSE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface VoicePageState {
    }


    /**
     * 语音记录状态
     */
    public static final int VOICE_RECORD_NORMAL = 0; // 普通状态(翻译成功)
    public static final int VOICE_RECORD_TRANSLATING = 1; // 翻译中
    public static final int VOICE_RECORD_FAILED = 2; // 翻译失败
    @IntDef({VOICE_RECORD_NORMAL, VOICE_RECORD_TRANSLATING, VOICE_RECORD_FAILED})
    @Retention(RetentionPolicy.SOURCE)
    public @interface VoiceRecordState {
    }

    /**
     * 服务器接口请求结果
     */
    public static class ResponseCode {
        public static final int OK = 10000;  // 成功
        public static final int ERROR_COMM = 401; //默认错误类型
        public static final int ERROR_TOKEN = 10060; //token错误
        public static final int PAY_WAIT = 20000; //wait for pay(支付等待)
        public static final int ERROR_SIGN = 10010; //datasign error(校验失败)

        public static final int ERROR_PARAM = 400; //请求参数错误
        public static final int ERROR_NEED_UPDATE = 112; //版本需要更新
        public static final int ERROR_SERVER = 500; //server error
        public static final int ERROR_REQUEST = 501; //request failed
        public static final int ERROR_NETWORK = 8989;//网络错误
        public static final int ERROR_IS_DANGER = 110;//字符涉嫌违规
        public static final int ERROR_SAME_LANG = 111;//字符涉嫌违规
    }

    public static final String paysuccessLogin = "paysuccessLogin";
    public static final String paysuccessFuntionblock = "paysuccessFuntionblock";
    public static final String PRODUCT_ID = "410";

    public static final String AppkeyAli = "l9VUnPUOuiSi0OPu";
    public static final String AppkeyAliEnglish = "3q8o1FSeUAkGOgwx";
    public static final String BRODCAST_ALI_RECORD_CONTINUE = "com.hundun.ali.token.continue.record.action";//获取阿里token后继续录音
    public static final String BRODCAST_ALI_TOKEN = "com.hundun.ali.token.action";//获取阿里token
    public static final String BRODCAST_SERVER_TIMER = "com.hundun.servertime.action";//获取阿里token后继续录音


    // Replace below with your own subscription key
    public static final String SpeechSubscriptionKey = "9c0ec18318fd4811a069eb2a49d71e3a";
    // Replace below with your own service region (e.g., "westus").
    public static final String SpeechRegion = "eastasia";
    public static final int FLAG_TTS_VOICE_REQUEST = 100006;//tts
    public static final int FLAG_TTS_VOICE_TEXT = 100007;//tts
    public static final String ACTION_WX_PAY_BACK = "cn.dongqi.wx.payback";
    public static final String BRODCAST_WXAUTHOR_ACTION = "com.dongqi.wxahour.action";//微信登录认证广播
    public static final String BRODCAST_LOGIN_ACTION = "com.dongqi.paper.login.action";//登录状态广播
    //    public static final String BRODCAST_COLSE_GUIDE_ACTION="com.hundun.paper.close.guide";//退出支付之前相关界面的认证广播
//    public static final String WX_APP_ID = "wxebb8cc5bc2dd01e4";
    public static final String WX_APP_ID = "wxb76ff530a7440def";
    public static final int FLAG_WATTING_REQUEST = 100004;//轮询间隔时间
    /**
     * TTS语音下载每次最多上传字符数
     */
    public static final int MAX_TTS_SIZE = 300;
    /**
     * 文本翻译每次做多上传字符数
     */
    public static final int MAX_TRANSLATE_SIZE = 2000;
    /**
     * 语音播放速度
     */
    public static final int VOICE_SPEED_MIN = 1;
    public static final int VOICE_SPEED_MAX = 15;

    public static final String COLLECTION_SHOW = "COLLECTION_SHOW";

    public static final String HISTORY_RECORD_ID = "HISTORY_RECORD_ID";
    public static final int TEXT_HISTORY_RESULT_OK = 10101;
    public static final int ALBUM_RESULT_OK = 10102;

    public static String fillMD5(String md5) {
        return md5.length() == 32 ? md5 : fillMD5("0" + md5);
    }

    public static String getMD5(String str) {
        if (str == null) {
            str = "";
        }
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            String md5 = new BigInteger(1, md.digest()).toString(16);
            //BigInteger会把0省略掉，需补全至32位
            return fillMD5(md5);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密错误:" + e.getMessage(), e);
        }
    }

    public static String getAliTokenParams(Context context, Map<String, Object> map) {
        TreeMap<String, Object> treeMap = new TreeMap<>(new Comparator<String>() {

            @Override
            public int compare(String s, String t1) {
                return s.compareTo(t1);
            }
        });
        if (map != null) {
            treeMap.putAll(map);
        }
        treeMap.put("deviceid", DeviceUtil.getDeviceId(context));
        long timeStampSec = CommonUtil.getServiceTimeStamp();
        String timestamp = String.format("%010d", timeStampSec);
        treeMap.put("timestamp", timestamp);
        String waitMD5 = "";
        for (Map.Entry<String, Object> entry : treeMap.entrySet()) {
            if (!"papertxt".equals(entry.getKey())) {
                waitMD5 += entry.getKey() + "=" + entry.getValue() + "&";
            }
        }
        String shortStr = waitMD5.substring(0, waitMD5.length() - 1);
        String signBefore = shortStr.concat("hUuPd20171206LuOnD");
        treeMap.put("datasign", getMD5(signBefore));
        return new Gson().toJson(treeMap).toString();
    }
}
